Skip to content

Conversation

romgrk
Copy link
Contributor

@romgrk romgrk commented Aug 12, 2025

Closes #19017

The effect was running too often because the container wasn't stable. Adding/removing the ResizeObserver seems to cause scroll jumps on firefox (and maybe chrome).

@romgrk romgrk added scope: data grid Changes related to the data grid. type: regression A bug, but worse, it used to behave as expected. labels Aug 12, 2025
@mui-bot
Copy link

mui-bot commented Aug 12, 2025

Deploy preview: https://deploy-preview-19156--material-ui-x.netlify.app/

Bundle size report

Bundle Parsed size Gzip size
@mui/x-data-grid ▼-34B(-0.01%) ▼-8B(-0.01%)
@mui/x-data-grid-pro ▼-34B(-0.01%) ▼-14B(-0.01%)
@mui/x-data-grid-premium ▼-34B(-0.01%) ▼-7B(0.00%)
@mui/x-charts 0B(0.00%) 0B(0.00%)
@mui/x-charts-pro 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers-pro 0B(0.00%) 0B(0.00%)
@mui/x-tree-view 0B(0.00%) 0B(0.00%)
@mui/x-tree-view-pro 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by 🚫 dangerJS against 9cfc478

@romgrk romgrk merged commit d233f08 into mui:master Aug 12, 2025
20 checks passed
@oliviertassinari oliviertassinari changed the title [datagrid] Fix scroll jumping [data grid] Fix scroll jumping Aug 12, 2025
@@ -259,7 +258,7 @@ function useDimensions(store: Store<BaseState>, params: VirtualizerParams, _api:
);
React.useEffect(() => debouncedUpdateDimensions?.clear, [debouncedUpdateDimensions]);

useLayoutEffect(() => observeRootNode(refs.container.current, store), [refs, store]);
useLayoutEffect(() => observeRootNode(containerNode, store), [containerNode, store]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't understand why it doesn't work as expected though:

const containerNode = refs.container.current;

console.log('render', containerNode);

React.useLayoutEffect(() => {
  console.log('useLayoutEffect', containerNode);
  return observeRootNode(containerNode, store);
}, [containerNode, store]);

Logs:

render null
dimensions.ts:261 render null
dimensions.ts:264 useLayoutEffect null
dimensions.ts:527 observeRootNode null
dimensions.ts:261 render null
dimensions.ts:261 render null
dimensions.ts:261 render <div class=​"MuiDataGrid-main MuiDataGrid-main--hiddenContent css-1bgckiu-MuiDataGrid-main" tabindex=​"-1" role=​"grid" aria-colcount=​"30" aria-rowcount=​"1" aria-multiselectable=​"true">​…​</div>​flex
dimensions.ts:261 render <div class=​"MuiDataGrid-main MuiDataGrid-main--hiddenContent css-1bgckiu-MuiDataGrid-main" tabindex=​"-1" role=​"grid" aria-colcount=​"30" aria-rowcount=​"1" aria-multiselectable=​"true">​…​</div>​flex
dimensions.ts:261 render <div class=​"MuiDataGrid-main MuiDataGrid-main--hiddenContent css-1bgckiu-MuiDataGrid-main" tabindex=​"-1" role=​"grid" aria-colcount=​"30" aria-rowcount=​"1" aria-multiselectable=​"true">​…​</div>​flex
dimensions.ts:261 render <div class=​"MuiDataGrid-main css-1bgckiu-MuiDataGrid-main" tabindex=​"-1" role=​"grid" aria-colcount=​"30" aria-rowcount=​"100001" aria-multiselectable=​"true">​…​</div>​flex
dimensions.ts:261 render <div class=​"MuiDataGrid-main css-1bgckiu-MuiDataGrid-main" tabindex=​"-1" role=​"grid" aria-colcount=​"30" aria-rowcount=​"100001" aria-multiselectable=​"true">​…​</div>​flex
dimensions.ts:261 render <div class=​"MuiDataGrid-main css-1bgckiu-MuiDataGrid-main" tabindex=​"-1" role=​"grid" aria-colcount=​"30" aria-rowcount=​"100001" aria-multiselectable=​"true">​…​</div>​flex
dimensions.ts:261 render <div class=​"MuiDataGrid-main css-1bgckiu-MuiDataGrid-main" tabindex=​"-1" role=​"grid" aria-colcount=​"30" aria-rowcount=​"100001" aria-multiselectable=​"true">​…​</div>​flex
dimensions.ts:261 render <div class=​"MuiDataGrid-main css-1bgckiu-MuiDataGrid-main" tabindex=​"-1" role=​"grid" aria-colcount=​"30" aria-rowcount=​"100001" aria-multiselectable=​"true">​…​</div>​flex

How come useLayoutEffect doesn't rerun when containerNode clearly changes and there are rerenders? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried moving this effect to the very top of the hook to make sure that it's not cancelled by another effect that has state update inside, but the behavior is the same.
This looks like a React bug to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or a nextjs bug – I can't reproduce the issue in stackblitz using the packages built from this branch: https://stackblitz.com/edit/kygkymrj?file=package.json

Copy link
Member

@cherniavskii cherniavskii Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commenting out this line makes layout effect run after containerNode changes and everything seems to work fine:

This means that the layout effect is indeed cancelled because of state update in useStoreEffect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, the issue doesn't appear for me in the playground page, it only appears in the other docs pages.

I'll refactor the state update away from the store effect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can reproduce it on playground page:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's your playground page setup? Mine works like this, which has been reported (by others) to avoid some nextjs issues, maybe I should configure it differently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that if I remove the shell that I have in my playground page, I observe the same issues that are otherwise present only in the rest of the pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: data grid Changes related to the data grid. type: regression A bug, but worse, it used to behave as expected.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[data grid] Scroll jumping in Firefox 141
3 participants